//
//  SearchViewController.m
//  BigToBig
//
//  Created by admin on 16/5/16.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "SearchViewController.h"
#import "PlayModel.h"
#import "PlayUserModel.h"
#import "PlayMoveViewController.h"
//static int p=1;
static int offset = 0;
static int each = 5;

@interface SearchViewController()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

{
    UIView *lineView;
    UIButton *SearchBtn;
    UISearchBar *searchBar;
    
}
@property (nonatomic ,strong)UITextField *titleText;
@property (nonatomic ,strong)UITableView *SearchTab;
@property (nonatomic ,strong)NSMutableArray *SearchArray;
@property (nonatomic ,strong)NSMutableArray *UserModelAray;
@property (nonatomic ,copy)NSString *strBtnTag;
@property (nonatomic ,assign)BOOL isHaveBtn;
@property (nonatomic ,weak)UIView *bigView;
@property (nonatomic ,weak)UIImageView *imageView;

@end
@implementation SearchViewController

- (void)viewDidLoad{

    [super viewDidLoad];
    
    
    NSMutableArray *SearchArray = [NSMutableArray array];
    self.SearchArray = SearchArray;
    
    NSMutableArray *userModelArray = [NSMutableArray array];
    self.UserModelAray = userModelArray;
    UITableView *SearchTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.SearchTab = SearchTab;
    self.SearchTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    SearchTab.delegate = self;
    SearchTab.dataSource = self;
    
    self.SearchTab.hidden = YES;
    [self.view addSubview:SearchTab];
    
    
    self.SearchTab.showsVerticalScrollIndicator = NO;
    
    self.SearchTab.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData1)];
    self.SearchTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    [self setBackBar];
    [self makeUI];
}
- (void)setBackBar{
        
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 20, 44);
    [backBtn addTarget:self action:@selector(backController) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"圆角矩形-1-副本"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
}

- (void)makeUI{

    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(252, 252, 252, 1);
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    titleView.layer.cornerRadius = 15;
    titleView.layer.masksToBounds = YES;
    
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth /4 * 3, 30)];
//    searchBar.placeholder = @"搜索项目";
    searchBar.layer.cornerRadius = 15;
    searchBar.layer.masksToBounds = YES;
    searchBar.layer.borderColor = COLOR_RGBA(91, 167, 252, 1).CGColor;
    searchBar.layer.borderWidth = 1;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth , 30)];
    [view addSubview:searchBar];
    [self.navigationItem setTitleView:view];
//    UITextField *titleText = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, titleView.width-125, 29)];
//    titleText.placeholder = @"搜索项目";
//    titleText.font = [UIFont systemFontOfSize:13.0];
//    titleText.textAlignment = NSTextAlignmentLeft;
//    titleText.clearButtonMode = UITextFieldViewModeWhileEditing;
//    titleText.delegate = self;
//    titleText.clearButtonMode = UITextFieldViewModeAlways;
//    self.titleText  = titleText;
//    [titleView addSubview:titleText];
//    
//    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(0, titleText.maxY, kScreenWidth - 40, 1)];
//    lineView1.backgroundColor = COLOR_RGBA(91, 167, 252, 1);
//
//    
//    UIButton *searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 120, 0, 60, 29)];
//    [titleView addSubview:searchBtn];
//    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
//    [searchBtn addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
//    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    self.navigationItem.titleView = titleView;
    
    [self addBtn];
    
}
- (void)backController{
    
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(88, 165, 255, 1);
}
-(void)loadData1{
    
    if (self.titleText.text.length == 0&&self.isHaveBtn == YES) {
        
        
        offset = offset + each;
        
        NSString *str = [NSString stringWithFormat:@"%d",offset];

        NSDictionary *params = @{@"category":self.strBtnTag,@"offset":str,@"each":@"5",@"site_id":@"0"};
        
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSLog(@"%@",json);
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
 
            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            
            [self.SearchTab.mj_footer endRefreshing];
//            [self.SearchTab.mj_header endRefreshing];
            
        } failure:^(NSError *error) {
            
            NSLog(@"%@",error.userInfo);
            

            [self.SearchTab reloadData];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                [self.SearchTab.mj_footer endRefreshing];
                
            });
            
            
        }];
        
        
    }else if(self.titleText.text.length !=0&&self.isHaveBtn==YES){
        
        offset = offset + each;
        
        NSString *str = [NSString stringWithFormat:@"%d",offset];

        
        NSDictionary *params = @{@"theme":self.titleText.text,@"category":self.strBtnTag,@"offset":str,@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];

            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            
            [self.SearchTab.mj_footer endRefreshing];
//            [self.SearchTab.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
//                [self.SearchTab.mj_header endRefreshing];
                
            });
            [self.SearchTab.mj_footer endRefreshing];

        }];
        
        
    }else if(self.titleText.text.length !=0 && self.isHaveBtn == NO){
        
        offset = offset + each;
        
        NSString *str = [NSString stringWithFormat:@"%d",offset];
        

        
        NSDictionary *params = @{@"theme":self.titleText.text,@"offset":str,@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            

            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            
            [self.SearchTab.mj_footer endRefreshing];
//            [self.SearchTab.mj_header endRefreshing];
            
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                [self.SearchTab.mj_footer endRefreshing];
//                [self.SearchTab.mj_header endRefreshing];
                
            });
        }];
        
    }else{
        [self.SearchTab.mj_footer endRefreshing];
//        [self.SearchTab.mj_header endRefreshing];
        
    }
    
    
}
-(void)loadData{

    [self.imageView removeFromSuperview];
    [self.SearchArray removeAllObjects];
    offset= 0;
    if (self.titleText.text.length == 0&&self.isHaveBtn == YES) {


        NSDictionary *params = @{@"category":self.strBtnTag,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSLog(@"%@",json);
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
//                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.SearchTab.bounds.size.height)];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                imageView.contentMode = UIViewContentModeCenter;

                self.imageView = imageView;
                [self.SearchTab addSubview:imageView];
            }
            
            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

            });
            
            [self.SearchTab.mj_header endRefreshing];

        } failure:^(NSError *error) {
            
            NSLog(@"%@",error.userInfo);
            [self.SearchTab.mj_header endRefreshing];

            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
//                  UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];

            });

            
        }];

        
    }else if(self.titleText.text.length !=0&&self.isHaveBtn==YES){

        NSDictionary *params = @{@"theme":self.titleText.text,@"category":self.strBtnTag,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                imageView.contentMode = UIViewContentModeCenter;

                self.imageView = imageView;
                [self.SearchTab addSubview:imageView];
            }
            
            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
           
            }
            [self.SearchTab.mj_header endRefreshing];

            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

            });
            
            
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];

            });
            [self.SearchTab.mj_header endRefreshing];

//            [self.SearchTab.mj_footer endRefreshing];
        }];
        

    }else if(self.titleText.text.length !=0 && self.isHaveBtn == NO){


        NSDictionary *params = @{@"theme":self.titleText.text,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                imageView.contentMode = UIViewContentModeCenter;

                self.imageView = imageView;

                [self.SearchTab addSubview:imageView];
            }
            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            [self.SearchTab.mj_header endRefreshing];

            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

            });
            
            
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];

                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];
                
            });
            
            [self.SearchTab.mj_header endRefreshing];
        }];

    }
    
//    else{
//        
//        [self.SearchTab.mj_header endRefreshing];
//        
//    }


}
- (void)search:(UIButton *)searchBtn{

    
    [self.imageView removeFromSuperview];
    searchBtn.selected = !searchBtn.selected;
    
    if (searchBtn.selected == YES) {
        
    self.titleText.userInteractionEnabled = NO;
    [self.titleText resignFirstResponder];
    [searchBtn setTitle:@"取消" forState:UIControlStateNormal];
    self.SearchTab.hidden = NO;
    [self.SearchTab reloadData];
    self.bigView.hidden = YES;
        
        [self.SearchArray removeAllObjects];
    if (self.titleText.text.length == 0&&self.isHaveBtn == YES) {
        
        NSDictionary *params = @{@"category":self.strBtnTag,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                imageView.contentMode = UIViewContentModeCenter;

                self.imageView = imageView;
                
                [self.SearchTab addSubview:imageView];
            }
            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
    
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            [self.SearchTab.mj_footer endRefreshing];
            [self.SearchTab.mj_header endRefreshing];
            
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];
                
                [self.SearchTab.mj_footer endRefreshing];
                [self.SearchTab.mj_header endRefreshing];
            });        }];
        
        
    }else if(self.titleText.text.length !=0&&self.isHaveBtn==YES){
    
        NSDictionary *params = @{@"theme":self.titleText.text,@"category":self.strBtnTag,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;

                [self.SearchTab addSubview:imageView];
            }

            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            
            [self.SearchTab.mj_footer endRefreshing];
            [self.SearchTab.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];
                
                [self.SearchTab.mj_footer endRefreshing];
                [self.SearchTab.mj_header endRefreshing];
            });

        }];

    }else if(self.titleText.text.length !=0 && self.isHaveBtn == NO){
    
        NSDictionary *params = @{@"theme":self.titleText.text,@"offset":@"0",@"each":@"5",@"site_id":@"0"};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/live/search?" params:params success:^(id json) {
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableArray *SearchArray = dict[@"live"];
            
            if (SearchArray.count == 0) {
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];

                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;

                [self.SearchTab addSubview:imageView];
            }

            for (int i = 0; i < SearchArray.count; i++) {
                
                NSDictionary *dict = SearchArray[i];
                
                NSMutableDictionary *userDict = dict[@"user"];
                PlayUserModel *JinRongUserMD = [PlayUserModel PlayUserModelWithDict:userDict];
                
                NSMutableDictionary *ObliveDict = dict[@"obj_live"];
                PlayModel *JinRongMD = [PlayModel PlayModelWithDict:ObliveDict];
                
                //字典转模型
                
                [self.SearchArray addObject:JinRongMD];
                [self.UserModelAray addObject:JinRongUserMD];
                
                NSLog(@"%@",JinRongMD.category);
                NSLog(@"%@",JinRongUserMD.userid);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
            });
            
            
            [self.SearchTab.mj_footer endRefreshing];
            [self.SearchTab.mj_header endRefreshing];
            
        } failure:^(NSError *error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.SearchTab reloadData];
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
                imageView.image = [UIImage imageNamed:@"not_data"];
                imageView.backgroundColor = [UIColor whiteColor];
                
                self.imageView = imageView;
                imageView.contentMode = UIViewContentModeCenter;
                [self.SearchTab addSubview:imageView];
                
                [self.SearchTab.mj_footer endRefreshing];
                [self.SearchTab.mj_header endRefreshing];
            });        }];
    
    }else{
    
        NSLog(@"这有个bug");
        
    }
        
    }else{

        self.isHaveBtn = NO;
        self.titleText.userInteractionEnabled = YES;
        self.titleText.text = nil;
        [SearchBtn removeFromSuperview];
        [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        self.SearchTab.hidden = YES;
        self.bigView.hidden = NO;
        [self.SearchArray removeAllObjects];

    }
}
- (void)addBtn{

    UIView *bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 264)];
    self.bigView = bigView;
    [self.view addSubview:bigView];
    UILabel *SearchLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 60)];
    SearchLabel.text = @"  热门搜索";
    SearchLabel.font = [UIFont systemFontOfSize:15];
    SearchLabel.textColor = [UIColor lightGrayColor];
    SearchLabel.textAlignment = NSTextAlignmentLeft;
    [bigView addSubview:SearchLabel];
//    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(0, SearchLabel.maxY, kScreenWidth, 1)];
//    lineView2.backgroundColor =[UIColor grayColor];
//    [bigView addSubview:lineView2];
    for (int i = 0; i < 6; i++) {
        
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake( 80*i + 10, 130, 60, 30)];
    btn.tag = i+21;
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    btn.layer.cornerRadius = 15;
    btn.layer.borderWidth = 1;
//    btn.layer.borderColor = [UIColor blueColor].CGColor;
        btn.layer.borderColor = COLOR_RGBA(91, 167, 252, 1).CGColor;
    [bigView addSubview:btn];

    if (i == 0) {
        [btn setTitle:@"金融" forState:UIControlStateNormal];
    
    }if (i == 1) {
        
        [btn setTitle:@"互联网" forState:UIControlStateNormal];
    }if (i == 2) {
        
        [btn setTitle:@"汽车" forState:UIControlStateNormal];
    }if (i == 3) {
        
        [btn setTitle:@"教育" forState:UIControlStateNormal];
    }if (i == 4) {
        
        btn.frame = CGRectMake(10, 170, 60, 30);
        [btn setTitle:@"健康" forState:UIControlStateNormal];
        
    }if (i == 5) {
        
        btn.frame = CGRectMake(90, 170, 60, 30);
        [btn setTitle:@"其它" forState:UIControlStateNormal];
    }
        NSInteger j = btn.titleLabel.text.length;
        btn.frame = CGRectMake(btn.x, btn.y, j * 20 + 10, btn.height);
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitleColor:COLOR_RGBA(91, 167, 252, 1) forState:UIControlStateNormal];
    }
}

- (void)BtnClick:(UIButton *)btn{

    [SearchBtn removeFromSuperview];
    
    SearchBtn  = [[UIButton alloc]initWithFrame:CGRectMake( 0,5,40, 20)];
    SearchBtn.frame = CGRectMake(5, 5, 40, 20);
    SearchBtn.backgroundColor = [UIColor whiteColor];
    SearchBtn.layer.cornerRadius = 10;
    SearchBtn.layer.borderWidth = 1;
    SearchBtn.layer.borderColor = COLOR_RGBA(91, 167, 252, 1).CGColor;
    [SearchBtn setTitleColor:COLOR_RGBA(91, 167, 252, 1) forState:UIControlStateNormal];
    SearchBtn.titleLabel.font = [UIFont systemFontOfSize:10.0];
    [SearchBtn setTitle:btn.titleLabel.text forState:UIControlStateNormal];

    SearchBtn.tag = btn.tag;
    NSString *strBtnTag = [NSString stringWithFormat:@"%ld",SearchBtn.tag];
    self.strBtnTag = strBtnTag;
    SearchBtn.selected = btn.selected;
    [self.titleText becomeFirstResponder];
    [self.titleText addSubview:SearchBtn];
    NSLog(@"5555555%ld",SearchBtn.tag);
    NSLog(@"加按钮");
    [searchBar becomeFirstResponder];
    [searchBar addSubview:SearchBtn];
    for (id obj in self.titleText.subviews)  {
    
    if ([obj isKindOfClass:[UIButton class]]) {
        
        UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
        self.titleText.leftView = paddingView1;
        self.titleText.leftViewMode = UITextFieldViewModeAlways;

        self.isHaveBtn = YES;
    }else{
    
    
        UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
        self.titleText.leftView = paddingView1;
        self.titleText.leftViewMode = UITextFieldViewModeAlways;
        self.isHaveBtn = NO;

    }


    }

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.SearchArray.count;
}


//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    //为了防止cell重用设置唯一的cellID
//    static  NSString *cellID = @"Cell";
//    
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    
//    if (cell==nil) {
//        
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//        
//    }
//    if (self.SearchArray.count > 0) {
//        
//        PlayModel *JinRongMD = self.SearchArray[indexPath.row];
//        NSLog(@"%@",JinRongMD.active_id);
//        //设置cell上的label
//        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(75, 17.5f, 100, 20)];
//        titleLb.font = [UIFont systemFontOfSize:12.0];
//        titleLb.text = JinRongMD.theme;
//        UILabel *VMRnumberLb = [[UILabel alloc]initWithFrame:CGRectMake(75, 37.5, 100, 20)];
//        VMRnumberLb.text = @"路演项目";
//        VMRnumberLb.font = [UIFont systemFontOfSize:12.0];
//        
//        UILabel *useCountLb = [[UILabel alloc]initWithFrame:CGRectMake(230, 15.0f, 100, 30)];
//        useCountLb.text = @"1.2万人次观看";
//        
//        useCountLb.font = [UIFont systemFontOfSize:12.0];
//        
//        //设置头像图片
//        UIImageView *imageView =[[UIImageView alloc]init];
//        [imageView setImage:[UIImage imageNamed:@"tx"]];
//        imageView.frame = CGRectMake(15.0f,7, 45, 45);
//        imageView.layer.masksToBounds = YES;
//        imageView.layer.cornerRadius = 27;
//        
//        
//        UIButton *PlayBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 61, kScreenWidth, 180)];
//        PlayBtn.tag = indexPath.row;
//        
//        
//        NSString *str = [NSString stringWithFormat:@"http://61.135.176.43:6080/data/FrontCover/%@",JinRongMD.front_cover];
//        NSLog(@"%@",JinRongMD.front_cover);
//        NSLog(@"%@",str);
//        
//        UIImageView *imageView1 = [[UIImageView alloc]init];
//        
//        [imageView1 sd_setImageWithURL:[NSURL URLWithString:str] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            
//            
//            [PlayBtn setImage:imageView1.image forState:UIControlStateNormal];
//            
//        }];
//        
//        NSLog(@"%@",imageView1.image);
//        [PlayBtn setImage:imageView1.image forState:UIControlStateNormal];
//        
//        [PlayBtn addTarget:self action:@selector(playScreen:) forControlEvents:UIControlEventTouchUpInside];
//        
//        lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, kScreenWidth, 1)];
//        lineView.backgroundColor = [UIColor grayColor];
//        
//        UIImageView *PlayView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 59)/2 , (PlayBtn.height - 59)/2, 59, 59)];
//        PlayView.image = [UIImage imageNamed:@"bf"];
//        [PlayBtn addSubview:PlayView];
//        
//        
//        [cell.contentView addSubview:PlayBtn];
//        [cell.contentView addSubview:lineView];
//        [cell.contentView addSubview:imageView];
//        [cell.contentView addSubview:VMRnumberLb];
//        [cell.contentView addSubview:titleLb];
//        [cell.contentView addSubview:useCountLb];
//    }
//    
//    
//    
//    
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    
//    return cell;
//    
//    
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //为了防止cell重用设置唯一的cellID
    NSString *cellID = [NSString stringWithFormat:@"Cell%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (cell==nil) {
        
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    if (self.SearchArray.count > 0) {
        
        PlayModel *JinRongMD = self.SearchArray[indexPath.row];
        PlayUserModel *JinRongUserMD = self.UserModelAray[indexPath.row];
        
        //设置cell上的label
        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(75, 17.5f, 100, 20)];
        titleLb.font = [UIFont systemFontOfSize:12.0];
        
        titleLb.text = JinRongMD.theme;
        UILabel *VMRnumberLb = [[UILabel alloc]initWithFrame:CGRectMake(75, 37.5, 100, 20)];
        
        VMRnumberLb.text = @"路演项目";
        VMRnumberLb.font = [UIFont systemFontOfSize:12.0];
        
        
        UILabel *useCountLb = [[UILabel alloc]initWithFrame:CGRectMake(230, 15.0f, 100, 30)];
        useCountLb.font = [UIFont systemFontOfSize:12.0];
        
        NSDictionary *params = @{@"activityId":JinRongMD.active_id};
        [HttpTool postWithURL:@"http://sdk2.karat.tv:8081/activity/onlineVeiwers?" params:params success:^(id json) {
            
            NSString *str = json[@"payload"];
            NSLog(@"%@",str);
            NSString *pepoleCountStr = [NSString stringWithFormat:@" %@人在线",str];
            useCountLb.text = pepoleCountStr;
            
        } failure:^(NSError *error) {
            
            
        }];
        
        
        //设置头像图片
        UIImageView *imageView =[[UIImageView alloc]init];
        imageView.frame = CGRectMake(15.0f,7, 45, 45);
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 27;
        
        NSString *uidStr = [NSString stringWithFormat:@"%@",JinRongUserMD.userid];
        NSLog(@"%@",uidStr);
        
        NSString *str1 = [self md5HexDigest:uidStr];
        NSString *b = [str1 substringWithRange:NSMakeRange(8,16)];
        NSString *str2 = @".png";
        NSString *imgName = [NSString stringWithFormat:@"%@%@",b,str2];
        NSLog(@"%@",imgName);
        NSString *HeadImageUrl = [NSString stringWithFormat:@"http://61.135.176.43:6080/data/HeadPortrait/%@",imgName];
        
        NSLog(@"%@",HeadImageUrl);
        
        
        UIImageView *imageView1 = [[UIImageView alloc]init];
        
        [imageView1 sd_setImageWithURL:[NSURL URLWithString:HeadImageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            //设置头像图片
            if (error) {
                
                [imageView setImage:[UIImage imageNamed:@"tx"]];
                
                return ;
            }
            imageView.image = imageView1.image;
            
            
        }];
        
        
        
        UIButton *PlayBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 61, kScreenWidth, 180)];
        PlayBtn.tag = indexPath.row;

        NSString *str = [NSString stringWithFormat:@"http://61.135.176.43:6080/data/FrontCover/%@",JinRongMD.front_cover];
        NSLog(@"%@",JinRongMD.front_cover);
        NSLog(@"%@",str);
        
        UIImageView *imageView2 = [[UIImageView alloc]init];
        [imageView2 sd_setImageWithURL:[NSURL URLWithString:str] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            
            [PlayBtn setImage:imageView2.image forState:UIControlStateNormal];
            
        }];
        
        [PlayBtn addTarget:self action:@selector(playScreen:)forControlEvents:UIControlEventTouchUpInside];
        
        
        lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 60, kScreenWidth, 1)];
        lineView.backgroundColor = [UIColor grayColor];
        
        UIImageView *PlayView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - 59)/2 , (PlayBtn.height - 59)/2, 59, 59)];
        PlayView.image = [UIImage imageNamed:@"bf"];
        [PlayBtn addSubview:PlayView];
        
        [cell.contentView addSubview:PlayBtn];
        [cell.contentView addSubview:lineView];
        [cell.contentView addSubview:imageView];
        [cell.contentView addSubview:VMRnumberLb];
        [cell.contentView addSubview:titleLb];
        [cell.contentView addSubview:useCountLb];
        
    }
    
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 240;
}
- (NSString *)md5HexDigest:(NSString*)password
{
    
    const char *original_str = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
    {
        [hash appendFormat:@"%02X", result[i]];
    }
    NSString *mdfiveString = [hash lowercaseString];
    
    NSLog(@"Encryption Result = %@",mdfiveString);
    return mdfiveString;
}

- (void)playScreen:(UIButton *)btn{
    
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    if (status == NotReachable) {
        
        
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请检查网络连接";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];
        
        return;
        
    }
    PlayUserModel *JinRongUserMD = self.UserModelAray[btn.tag];
    PlayModel *JinRongModel = self.SearchArray[btn.tag];
    
    PlayMoveViewController *playMove = [[PlayMoveViewController alloc]init];
    NSString *playActive_id = [NSString stringWithFormat:@"%@",JinRongModel.active_id];
    playMove.playStr = playActive_id;
    ;
    
    NSLog(@"%@",playMove.playStr);
    
    
    playMove.userID = JinRongUserMD.userid;
    
    playMove.nameStr = JinRongUserMD.name;
    playMove.origentionScreen = JinRongModel.transform;

    [[(MainTabBarController *)KeyWindow.rootViewController selectedViewController]presentViewController:playMove animated:YES completion:nil];
    
    NSLog(@"播放视频");
    
    
}
//- (void)textFieldDidDeleteBackward:(UITextField *)textField{
//
//    NSLog(@"fsdfs");
//}
//
//- (void)deleteBackward{
//    
//    NSLog(@"fsdfs");
//}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    NSLog(@"%@",self.titleText);
    if (self.isHaveBtn) {
        UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
        self.titleText.leftView = paddingView1;
        self.titleText.leftViewMode = UITextFieldViewModeAlways;
        
    }else{
    
        UIView *paddingView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
        self.titleText.leftView = paddingView1;
        self.titleText.leftViewMode = UITextFieldViewModeAlways;
        

    }


    return YES;
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.titleText endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.titleText endEditing:YES];
    return YES;
}

@end
